# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.native_public_client_base as npubcb


class FcoinNativePublicClient(npubcb.NativePublicClientBase):
  def __init__(self, timeout=5):
    super().__init__(url='https://api.fcoin.com/v2', timeout=timeout)

  def get_server_time(self):
    url = 'public/server-time'
    update = self._get_as_json(url)
    return update

  def get_currencies(self):
    url = 'public/currencies'
    update = self._get_as_json(url)
    return update

  def get_symbols(self):
    url = 'public/symbols'
    update = self._get_as_json(url)
    return update

  def get_ticker(self, symbol):
    url = 'market/ticker/{symbol}'
    url = url.format(symbol=symbol)
    update = self._get_as_json(url)
    return update

  def get_depth(self, symbol, level='L20'):
    assert level in ['L20', 'L100', 'full'], level
    url = 'market/depth/{level}/{symbol}'
    url = url.format(level=level, symbol=symbol)
    update = self._get_as_json(url)
    return update

  def get_trade(self, symbol, before=None, limit=None):
    url = 'market/trades/{symbol}'
    url = url.format(symbol=symbol)
    params = {
        "before": before,
        "limit": limit,
    }
    update = self._get_as_json(url, params)
    return update

  def get_candle(self, symbol, resolution='M1', before=None, limit=None):
    assert resolution in ['M1', 'M3', 'M5', 'M15', 'M30', 'H1', 'H4', 'H6', 'D1', 'W1',
                          'MN'], resolution
    url = 'market/candles/{resolution}/{symbol}'
    url = url.format(resolution=resolution, symbol=symbol)
    params = {
        "before": before,
        "limit": limit,
    }
    update = self._get_as_json(url, params)
    return update

  def queryAll(self):
    symbol = 'ftbtc'
    print(client.get_server_time())
    print(client.get_currencies())
    print(client.get_symbols())
    print(client.get_ticker(symbol))
    print(client.get_depth(symbol))
    print(client.get_trade(symbol))
    print(client.get_candle(symbol))


if __name__ == "__main__":
  client = FcoinNativePublicClient(timeout=10)
  client.queryAll()
